{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模块和包\n",
    "\n",
    "模块是Python源代码文件，即扩展名为.py的文件。\n",
    "\n",
    "软件包是一个包含`__init__.py`文件的目录，并且可以包含python模块和其他软件包。\n",
    "\n",
    "\n",
    "#### 为什么要将代码组织到模块和包中\n",
    "\n",
    "1. 可维护性\n",
    "2. 可重用性\n",
    "3. 命名空间\n",
    "4. 仅仅通过查看项目的目录结构，不熟悉您项目的人就可以获得清晰的概述。\n",
    "5. 搜索某些功能或类很容易"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如何使用\n",
    "\n",
    "让我们以以下目录结构为例, 假设我们已经写好了一个水果店软件。\n",
    "\n",
    "```\n",
    "新建文件夹：food_store/\n",
    "    新建文件__init__.py\n",
    "    新建文件banana.py\n",
    "\n",
    "```\n",
    "\n",
    "水果店软件中的香蕉（banana.py）文件包含："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "BASE_PRICE = 10 # 价格\n",
    "\n",
    "# 定义了一个获取香蕉品牌的函数\n",
    "def get_available_brands():\n",
    "    # 香蕉品牌\n",
    "    return ['chiquita','百果园','华圣']\n",
    "\n",
    "# 定义价格计算公式\n",
    "def get_price(discount):\n",
    "    return BASE_PRICE*discount\n",
    "\n",
    "# 定义了一个香蕉类\n",
    "class Banana:\n",
    "    def __init__(self, brand='百果园'):\n",
    "        if brand not in get_available_brands():\n",
    "            raise ValueError('Unkown brand: {}'.format(brand))\n",
    "        self.__brand = brand\n",
    "    \n",
    "    def print_info(self):\n",
    "        print(\"品牌是\",self.__brand)\n",
    "```\n",
    "\n",
    "### Import 引入自定义模块\n",
    "\n",
    "引入模块中某个功能或类，可以指定其中一个引入，用`from...import`把Banana类引入，重复使用这个香蕉类。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from food_store.banana import Banana #引入Banana类（部分）\n",
    "\n",
    "# 使用Banana类创建实例，这样我们就可以重复使用以前写过的代码。\n",
    "my_banana = Banana()\n",
    "my_banana.print_info() # 输出信息"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "引入整个模块，注意对比观察，两个例子有什么不一样。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from food_store import banana  #引入banana模块（全部）\n",
    "\n",
    "# 使用获取品牌函数\n",
    "brands = banana.get_available_brands()\n",
    "print(\"所有品牌\",brands)\n",
    "# 使用Banana类创建实例\n",
    "my_banana = banana.Banana()\n",
    "my_banana.print_info() # 输出信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "banana.get_available_brands"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 练习1 \n",
    "1.以上例子输出：品牌是华圣"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 练习2\n",
    "\n",
    "1. 引入`BASE_PRICE`价格\n",
    "2. 把BASE_PRICE改为9\n",
    "3. 引入计算`get_price`价格函数\n",
    "4. 计算8折价格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#要求：引入BASE_PRICE价格，输出默认价格\n",
    "from food_store.banana import ？？？\n",
    "\n",
    "print('价格:',？？？)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#要求：引入BASE_PRICE价格，并把BASE_PRICE改为9\n",
    "from food_store.banana import ？？？\n",
    "\n",
    "？？？？？\n",
    "\n",
    "print('价格:',？？？)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#要求：引入计算get_price价格函数，并计算8折价格\n",
    "\n",
    "from food_store.banana import ？？？\n",
    "\n",
    "print('8折价格:',？？？(？))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
